$(document).ready(function(){
	
});
function getAllEvents(){
	activeNav('EventListTab');
	$.ajax({
		type: 	"GET",
		url:	"getEventListAjax?set_ajax_view",
		success: function(data){
			$("#pageContent").html(data);
			$("#pageContent").hide();
			$("#pageContent").show('slow');	
		}
	});
}

function renderMonth(year, month) {
	activeNav('monthView');
	$.ajax({
		type: 	"GET",
		url:	"renderMonth?set_ajax_view",
		data: 	"year=" + year + "&month=" + month,
		success: function(data){
			$("#calendarView").html(data);		
		}
	});
}

function renderWeek(year, month, day) {
	activeNav('weekView');
	$.ajax({
		type: 	"GET",
		url:	"renderWeek?set_ajax_view",
		data: 	"year=" + year + "&month=" + month + "&day=" + day,
		success: function(data){
			$("#calendarView").html(data);		
		}
	});
}

function renderDay(year, month, day) {
	activeNav('dayView');
	$.ajax({
		type: 	"GET",
		url:	"renderDay?set_ajax_view",
		data: 	"year=" + year + "&month=" + month + "&day=" + day,
		success: function(data){
			$("#calendarView").html(data);		
		}
	});
}

function activeNav(linkId) {
	$('#calNav a').removeClass('active');
	$('#' + linkId).addClass('active')
}

function createEvent(eventId){
	activeNav('createEventTab');
	$.ajax({
		type:	"GET",
		url:	"createEventAjax?set_ajax_view",
		data:	"eventId=" + eventId ,
		success: function(data){
		
		$("#pageContent").html(data);
		$("#pageContent").hide();
		$("#pageContent").show('slow');
		//alert(userArray.length);
		//for(i=0; i < userArray.length; i++){alert(userArray[i]);}
			// Enable date-picker
		$('.date-pick').datePicker()
		$('#start-date').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#end-date').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#end-date').bind(
			'dpClosed',
			function(e, selectedDates)
			{
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#start-date').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);

		}
	});
}

function saveEvent(){
	
	if(!isNotEmpty($('#name'),"Vui lòng nhập tên.","status_message")){
		return false;
	}
	if(!checkTime()) return false;
	var submit_data = $('#create_event_form').serialize();
	$.ajax({
		type:	"GET",
		url:	"saveEventAjax?set_ajax_view",
		data: 	submit_data + "&userArray=" + userArray.join(',') ,
		success: function(data){
			
		}
	});
}

function checkDates(){
	$('#status_message').html('');
	var startDate = checkFullDate($('#startDate'),'','','');
	var endDate = checkFullDate($('#endDate'),'','','');
	if (!startDate) {
		$('#startDate').css("border-color", "red");
		$('#status_message').html("Ngày bắt đầu không hợp lệ");
		$('#status_message').show();
		return false;
	}else{
		$('#startDate').css("border-color", "#AAA");
		$('#status_message').hide();
	}
	if (!endDate){
		$('#endDate').css("border-color", "red");
		$('#status_message').html("Ngày kết thúc không hợp lệ");
		$('#status_message').show();
		return false;
	}else{
		$('#endDate').css("border-color", "#AAA");
		$('#status_message').hide();
	}
	var start = $('#startDate').val().split('/');
	var end = $('#endDate').val().split('/');
	if (new Date(start[2], start[1], start[0]) > new Date(end[2], end[1], end[0])){
		$('#startDate').css("border-color", "red");
		$('#endDate').css("border-color", "red");
		$('#status_message').html("Ngày bắt đầu phải trước ngày kết thúc");
		$('#status_message').show();
		return false;
	}else{
		$('#startDate').css("border-color", "#AAA");
		$('#endDate').css("border-color", "#AAA");
		$('#status_message').hide();
	}
	return true;
}

function checkTime(){
	if(!checkDates()) return false; 
	//check startHour
	var startHour = $.trim($('#startHour').val());
	if(!isInteger(startHour) || startHour < 0 || startHour > 23 ){
		$('#startHour').css("border-color", "red");
		$('#status_message').text("Thời gian không hợp lệ.");
		$('#status_message').show();
		return false;
	}else{
		$('#startHour').css("border-color", "#AAA");
		$('#status_message').text("");
		$('#status_message').hide();
	}
	//check startMinute
	var startMinute = $.trim($('#startMinute').val());
	if(!isInteger(startMinute) || startMinute < 0 || startMinute > 59 ){
		$('#startMinute').css("border-color", "red");
		$('#status_message').text("Thời gian không hợp lệ.");
		$('#status_message').show();
		return false;
	}else{
		$('input#startMinute').css("border-color", "#AAA");
		$('span#status_message').text("");
		$('span#status_message').hide();
	}
	//check endHour
	var endHour = $.trim($('#endHour').val());
	if(!isInteger(endHour) || endHour < 0 || endHour > 23 ){
		$('input#endHour').css("border-color", "red");
		$('span#status_message').text("Thời gian không hợp lệ.");
		$('span#status_message').show();
		return false;
	}else{
		$('input#endHour').css("border-color", "#AAA");
		$('span#status_message').text("");
		$('span#status_message').hide();
	}
	//check endMinute
	var endMinute = $.trim($('#endMinute').val());
	if(!isInteger(endMinute) || endMinute < 0 || endMinute > 59 ){
		$('input#endMinute').css("border-color", "red");
		$('span#status_message').text("Thời gian không hợp lệ.");
		$('span#status_message').show();
		return false;
	}else{
		$('input#endMinute').css("border-color", "#AAA");
		$('span#status_message').text("");
		$('span#status_message').hide();
	}
	//check endTime > startTime
	if($('#startDate').val() == $('#endDate').val()){
		if(parseInt(endHour) < parseInt(startHour)){
			$('input#startHour').css("border-color", "red");
			$('input#endHour').css("border-color", "red");
			$('span#status_message').text("Thời gian kết thúc không được nhỏ hơn thời gian bắt đầu.");
			$('span#status_message').show();
			return false;
		}else if(startHour == endHour){
			if(parseInt(endMinute) < parseInt(startMinute)){
				$('input#startMinute').css("border-color", "red");
				$('input#endMinute').css("border-color", "red");
				$('span#status_message').text("Thời gian kết thúc không được nhỏ hơn thời gian bắt đầu.");
				$('span#status_message').show();
				return false;
			}
		}
	}
	return true;
}
function map_event_users(eventId){
	$.ajax({
		type:	"GET",
		url:	"MapEventUserAjax?set_ajax_view",
		data: 	"eventId=" + eventId ,
		success: function(data){
			$("#EvenListContent").hide('slow');
			$("#MapEventUserContent").html(data);
			$("#MapEventUserContent").show('slow');
		}
	});
}

function searchUser(){
	var str = $('#searchString').val();		
	while (str.indexOf('  ') != '-1'){
		$('#searchString').val(str.split('  ').join(' '));
		str = $('#searchString').val();
	}
	if ($.trim($('#searchString').val()) != "") {
		$('#SearchBtnDiv').css('display', 'none');
		$('#searchLoading').css('display', 'block');
		$.ajax({
			type:	"GET",
			url:	"SearchUserAjax?set_ajax_view",
			data: 	"searchStr=" + $('#searchString').val() + "&employeeTypeId=" + $('#employee_type_search').val() ,
			success: function(data){
				$('#error_search').text("");
				$('#searchString').css('border-color', '#AAA'); 
				if(data == ''){
					$('#searchResult').hide('');
					$('#SearchBtnDiv').css('display', 'inline');
					$('#searchLoading').css('display', 'none');
					$('#search_message').text(_("Result not found."));
				}else{
					$('#search_message').text('');
					$("#searchResultTbl tbody").html('');
					$("#searchResultTbl tbody").append(data);
					$('#SearchBtnDiv').css('display', 'inline');
					$('#searchLoading').css('display', 'none');
					$('#searchResult').show('slow');
				}
			}
		});
		return;
	}
	$('#error_search').text("Vui lòng nhập từ khoá.");
	$('#searchString').css('border-color', 'red'); 
}

function addUser(userId, rowId){
	var name = $('#'+userId+'_name_span').text();
	var employee_type = $('#'+userId+'_employee_type_span').text();
	var data = "<tr id="+userId+"_row><td class='first'>"+name+"<input type='hidden' name='user[]' value='"+userId+"'/></td>"
	     + "<td>"+employee_type+"</td>"
	 	 + "<td center class='last'><a href='javascript:removeUser("+userId+");'>" + _("Unselect") + </a></td></tr>";
	$('#user_tbl tbody').append(data);
	$('#'+rowId+'_row').remove();
	$('#user_list_tbl').hide();
	$('#user_list_tbl').show();
	userArray.push(userId);
	if(action == 'mapping'){
		$.ajax({
			type:	"GET",
			url:	"addUserToRelationshipAjax?set_ajax_view",
			data:	"relational_personId=" + userId + "&personId=" + $('#select_employee').val(),
			success: function(data){
				
			}
		});
	}

}
function removeUser(userId){
	if(action == 'mapping'){
		$.ajax({
			type:	"GET",
			url:	"RemoveUserFromRelationshipAjax?set_ajax_view",
			data:	"relational_personId=" + userId + "&personId=" + $('#select_employee').val(),
			success: function(data){
				
			}
		});
	}
	$('#'+userId+'_row').remove();
	userArray.pop(userId);
}

function deleteEvent(eventId){
	$.ajax({
		type:	"GET",
		url:	"deleteEventAjax?set_ajax_view",
		data: 	"eventId=" + eventId ,
		success: function(data){
			$("#event_list_tbl tbody").html('');
			$("#event_list_tbl tbody").append(data);
			$("#event_list_tbl").show();
		}
	});
}

function cancelEvent(){
	getAllEvents();
}

function viewDetailOfEvent(eventId){
	$.ajax({
		type:	"GET",
		url:	"getDetailOfEventAjax?set_ajax_view",
		data:	"eventId=" + eventId,
		success: function(data){
			$('#calendarView').html(data);
		}
	});
}
function mappingRelationship(){
	activeNav('mappingRelationshipTab');
	$.ajax({
		type:	"GET",
		url:	"MappingRelationshipAjax?set_ajax_view",
		success: function(data){
			$('#pageContent').html(data);
			$('#pageContent').show('slow');
			getUsersByEmployeeType();
			
		}
	});
}

function getUsersByEmployeeType(){
	$('#search_div').hide();
	$('#user_tbl tbody').html('');
	var employee_type_id = $('#employee_type').val();
	$.ajax({
		type:	"GET",
		url:	"GetUserByEmployeeTypeAjax?set_ajax_view",
		data:	"employee_type_id=" + employee_type_id ,
		success: function(data){
			$('#select_employee').html('');
			$('#select_employee').append("<option id='' value=''>-- " + _("Select employee") + " --</option>");
			$('#select_employee').append(data);
		}
	});
}

function getRelationalUsers(){
	var employeeId = $('#select_employee').val();
	if(employeeId == ''){
		$('#search_div').hide();
		$('#user_tbl tbody').html('');
		return false;
	}
	$('#search_div').show();
	$.ajax({
		type:	"GET",
		url:	"GetRelationalUserAjax?set_ajax_view",
		data:	"employeeId=" + employeeId ,
		success: function(data){
			$('#user_tbl tbody').html('');
			$('#user_tbl tbody').append(data);
		}
	});
}

function choose_recursive_type(){
	var selected = $('#select_recursive_type').val();
	switch (selected){
	case '2':
		$('#recursive_num_div').show();
		$('#check_dayWeek_div').show();
		$('#recursive_type_text').text(_("week");
		break;
	case '3':
		$('#recursive_num_div').show();
		$('#check_dayWeek_div').hide();
		$('#recursive_type_text').text(_("month"));
		break;
	default:
		$('#recursive_num_div').hide();
		$('#check_dayWeek_div').hide();
		break;
	} 	
}